<?php

namespace App\Http\Services;

use App\Models\Company;
use App\Models\JobColumn;
use App\Models\Recruit;
use Auth;
use Illuminate\Support\Facades\Log;

class RecruitService
{
    public function update($item)
    {
        $data = new Recruit();
        if(isset($item['id']) && $item['id']){
            $data = Recruit::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        //发布职位绑定企业
        $data->title = $item['title'];
        $data->full_name = $item['full_name'] ?? '';
        $data->education = $item['education'] ?? 0;
        $data->experience = $item['experience'] ?? 0;
        $data->content = $item['content'];
        $data->internship_month = $item['internship_month'] ?? 0;
        $data->internship_day = $item['internship_day'] ?? 0;
        $data->min_wages = $item['min_wages'] ?? 0;
        $data->max_wages = $item['max_wages'] ?? 0;
        $data->address = $item['address'] ?? '--';
        $data->province = $item['province'] ?? '--';
        $data->city = $item['city'] ?? '--';
        $data->areas = $item['areas'] ?? '--';
        $data->post_key = !empty($item['post_key']) ? json_encode($item['post_key']) : json_encode([]);
        $data->recruit_type = $item['recruit_type'] ?? 1;
        $data->salary_type = $item['salary_type'] ?? 1;
        $data->salary = $item['salary'] ?? 1;
        $data->type = $item['type'] ?? 0;
        $data->eid = $item['eid'] ?? 0;
        $data->resource_id = $item['resource_id'];
        $data->update_time = time();
        $data->is_recommend = $item['is_recommend'] ?? 0;
        try {
            $data->save();
            if($data->id){
                return true;
            }
        } catch (\Throwable $th) {
            Log::info($th);
            return  false;
        }
    }
    
    public function companyUpdate($item)
    {
        $uid = 0;
        $eid = 0;
        $data = new Company();
        if(isset($item['uid']) && $item['uid'] > 0){
            if(isset($item['eid']) && $item['eid'] > 0){
                $eid = $item['eid'];
                $data = Company::where('eid',$item['eid'])->first();
            }else{
                $uid = $item['uid'];
                $data = Company::where('uid',$item['uid'])->first();
            }
            
            if(!$data){
                $data = new Company();
                $data->create_time = time();
            }
        }else{
            $data->create_time = time();
        }
        $data->log = $item['log'];
        $data->full_name = $item['full_name'];
        $data->introduce = $item['introduce'] ?? '';
        $data->images = $item['images'] ?? '[]';
        $data->work_start = $item['work_start'] ?? '';
        $data->work_end = $item['work_end'] ?? '';
        $data->province = $item['province'] ?? '--';
        $data->city = $item['city'] ?? '--';
        $data->areas = $item['areas'] ?? '--';
        $data->address = $item['address'] ?? '--';
        $data->financing = $item['financing'] ?? 0;
        $data->scale = $item['scale'] ?? 0;
        $data->uid = $uid;
        $data->eid = $eid;
        $data->overtime = $item['overtime'] ?? 0;
        $data->legal_person = $item['legal_person'] ?? '';
        $data->registered_capital = $item['registered_capital'] ?? '';
        $data->registered_date = $item['registered_date'] ?? '';
        $data->company_type = $item['company_type'] ?? '';
        $data->company_state = $item['company_state'] ?? '';
        $data->company_code = $item['company_code'] ?? '';
        $data->licence = $item['licence'] ?? '';
        $data->welfare = $item['welfare'] ?? '';
        $data->update_time = time();
        try {
            $data->save();
            if($data->id){
                return $data->id;
            }
        } catch (\Throwable $th) {
            Log::info($th);
            return false;
        }
    }

    public function updateType($item)
    {
        $data = new JobColumn();
        if(isset($item['id']) && $item['id'] > 0){
            $data = JobColumn::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        $data->title = $item['title'];
        $data->torder = $item['torder'] ?? 0;
        $data->pid = $item['pid'] ?? 0;
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    
}
